<?php
/**
 * Created by PhpStorm.
 * User: 15272
 * Date: 2019/1/6
 * Time: 11:38
 */

namespace app\user\controller;


use app\admin\model\CodePasswordModel;
use app\admin\model\GoodsModel;
use app\admin\model\NavMenuModel;
use app\admin\model\ShopGoodsModel;
use cmf\controller\UserBaseController;
use think\Db;

class GoodsController extends UserBaseController
{
    public function index()
    {

        $this->view->goods_type = (new NavMenuModel())->getGoodsType();
        if(request()->isAjax()){
            $where = [];
             if($val = input('goods_type/n')){
                 $where['goods.parent_id'] =$val;
             }
             if($val = input('goods_keyword/s')){
                 $where['goods.g_name'] = ['like',"%{$val}%"];
             }
            $data = Db::name('goods')
                ->alias('goods')
                ->whereNull('goods.delete_time')/*没有删除的产品*/
                ->join('shop_goods shop','shop.goods_id = goods.g_id and shop.user_id='.cmf_get_current_user_id().'','left')/*链接商家定价表*/
                ->where($where)
                ->select();
            $this->success('','',$data);
        }

        return $this->fetch();
    }
    
    //设置价格
    public function setMoney()
    {

        $money = input('money');//修改的价格
        $goods_id = input('goods_id/n');//产品id

        //验证价格格式
        if(!preg_match("/^\d{1,}\.*\d{0,2}$/",$money)){
            $this->error('正确格式为0.00');
        }
        if($money < 0){
            $this->error('不能小于零');
        }



        //查询存在不
        $data = ShopGoodsModel::where('goods_id',$goods_id)/*对应的产品*/
        ->where('user_id',cmf_get_current_user_id())/*是自己的*/
        ->find();

        $is_goods_del =  GoodsModel::whereNull('delete_time')->where('g_id',$goods_id)->find();

        //验证价格存在不
        $money_sum = $money+$is_goods_del['basics_money'];
        /*if(!in_array($money_sum,config('card_password_money'))){
            $this->error($money_sum.'元的支付码不存在，请联系客服，目前已有的支付码为：'.implode(',元',config('card_password_money'))).'元';
        }*/


        if($data){
            //更新
            $data->money = $money;
            $data->save();
        }else{
            //验证产品 必须要存在

           if(!$is_goods_del){
               $this->error('产品不存在');
           }
            $db = new ShopGoodsModel();
            $db->save([
               'money'=>$money
                ,'user_id'=>cmf_get_current_user_id()
                ,'create_time'=>time()
                ,'update_tile'=>time()
                ,'goods_id'=>$goods_id
            ]);
        }

        $this->success('编程成功');
    }

    //上下架

    public function setGoodsType()
    {
        $goods_id = input('goods_id/n');
        $goods_type = input('goods_type/n');
        if(!in_array($goods_type,[0,1])){
            $this->error('类型不存在');
        }

        //检测产品存在不
        $data = ShopGoodsModel::where('goods_id',$goods_id)/*对应的产品*/
        ->where('user_id',cmf_get_current_user_id())/*是自己的*/
        ->find();
        if(!$data){
            $this->error('请先设置价格');
        }

        $data->shop_goods_state = $goods_type;
        $data->save();
        $this->success('编辑成功');
    }
}